﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using todo.Model;
using todo.Ribbon;

namespace todo.Plugin
{
    public class PluginManager
    {
        public static PluginManager Instance = new PluginManager();
        private List<IPlugin> _plugins;

        public void Initialize()
        {
            var plugins = new List<IPlugin>();
            var Directory = new DirectoryInfo(App.Path);
            var files = Directory.GetFiles("*.dll");
            if (files.Length > 0)
            {
                foreach (var fi in files)
                {
                    var assemblyPlugins = LoadPluginsFromAssembly(fi.FullName);
                    if (assemblyPlugins != null && assemblyPlugins.Count > 0)
                    {
                        plugins.AddRange(assemblyPlugins);
                    }
                }
            }
            _plugins = plugins;
            //
            foreach (var plugin in _plugins)
            {
                plugin.Initialize();
            }
        }

        private static List<IPlugin> LoadPluginsFromAssembly(string fullName)
        {
            var plugins = new List<IPlugin>();
            var pluginType = typeof (IPlugin);

            var assembly = Assembly.LoadFrom(fullName);
            if (assembly == null)
            {
                return plugins;
            }
            var types = assembly.GetExportedTypes();
            foreach (var t in types)
            {
                if (!t.IsClass || t.IsNotPublic)
                {
                    continue;
                }

                if (pluginType.IsAssignableFrom(t))
                {
                    var plugin = Activator.CreateInstance(t) as IPlugin;
                    if (plugin != null)
                    {
                        plugins.Add(plugin);
                    }
                }
            }
            return plugins;
        }

        public void OnRibbonInited(RibbonUI ribbonUi)
        {
            foreach (var plugin in _plugins)
            {
                plugin.OnRibbonInited(ribbonUi);
            }
        }

        public bool ContainsPlugin(string name)
        {
            foreach (var plugin in _plugins)
            {
                if (plugin.Name == name)
                {
                    return true;
                }
            }
            return false;
        }

        public void OnSave(string file, TodoList todoList)
        {
            foreach (var plugin in _plugins)
            {
                plugin.OnSave(file, todoList);
            }
        }

        public void OnOpen(string fileName)
        {
            foreach (var plugin in _plugins)
            {
                plugin.OnOpen(fileName);
            }
        }

        public void OnClassicUIInited()
        {
            foreach (var plugin in _plugins)
            {
                plugin.OnClassicUIInited();
            }           
        }
    }
}